package com.dao;

import com.po.FriendList;
import com.po.InBox;
import com.utils.JDBCUnits;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InboxDao {
    /**
     * target_id这个用户将接受到user_id的信息，这个信息可能有很多方面
     * @param user_id 自己的id
     * @param target_id 发送给对方的id
     * @param way 发送的是什么，有可能是给了私信，也有可能是点赞，也有可能是感谢
     * @param inbox_content 内容，可以是感谢的内容等
     */
    public static void inboxTip(int user_id , int target_id ,String way ,String inbox_content){
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "insert into inbox(user_id,target_id,content,way) values(?,?,?,?)";
            ps = c.prepareStatement(sql);
            ps.setInt(1,user_id);
            ps.setInt(2,target_id);
            ps.setString(3,inbox_content);
            ps.setString(4,way);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps);
        }
    }

    /**
     * 当用户查看自己的收件箱时，可以把邮件删掉
     * @param user_id 自己的id
     * @param id 对方的id
     * @param way 删除哪种方式的私信
     */
    public static void deleteAdd(int user_id, int id, String way) {
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "delete from inbox where user_id=? and target_id=? and way=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1, user_id);
            ps.setInt(2, id);
            ps.setString(3, way);
            int i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUnits.close(c, ps);
        }
    }

    /**
     * 查看该请求是否存在，如果存在则无法继续发送请求
     * @param id
     * @param user_id
     * @param way
     * @return
     */
    public static boolean ifExit(int id, int user_id, String way) {
        boolean success = true;
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from inbox where user_id=? and target_id=? and way = ?";
            ps = c.prepareStatement(sql);
            ps.setInt(1, id);
            ps.setInt(2, user_id);
            ps.setString(3, way);
            rs = ps.executeQuery();
            if(rs.next()){
                success = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUnits.close(c, ps, rs);
        }

        return success;
    }

    /**
     * 获取
     * @param id
     * @param user_id
     * @param way
     * @return
     */
    public static int InsertThanks(int id, int user_id, String way) {
        Connection c = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            if (ifExit(id, user_id, way)) {
                c = JDBCUnits.getConnectino();
                String sql = "insert into inbox(user_id,target_id,way) values(?,?,?)";
                ps = c.prepareStatement(sql);
                ps.setInt(1, id);
                ps.setInt(2, user_id);
                ps.setString(3, way);
                i = ps.executeUpdate();
            }else{
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUnits.close(c, ps);
        }
        return i;
    }

    /**
     * 获取我的邮件
     * @param id
     * @return
     */
    public static List<InBox> getMyNote(int id) {
        List<InBox> myInbox = new ArrayList<>();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from inbox where target_id = ?";
            ps = c.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                InBox box = new InBox();
                box.setContent(rs.getString("content"));
                box.setWay(rs.getString("way"));
                box.setUser_id(rs.getInt("user_id"));
                box.setName(FriendList.getUser(rs.getInt("user_id")).getName());
                box.setSex(FriendList.getUser(rs.getInt("user_id")).getSex());
                myInbox.add(box);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUnits.close(c, ps, rs);
        }

        return myInbox;
    }
}
